﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace UEditor
{
    public static class Config
    {
        private static bool noCache = true;
        private static Dictionary<string, object> BuildItems()
        {
            string json = Regex.Replace(File.ReadAllText(HttpContext.Current.Server.MapPath("config.json")), @"(/\*.*?\*/)|(\r\n)", "");
            return (new JavaScriptSerializer()).DeserializeObject(json) as Dictionary<string, object>;
        }

        public static Dictionary<string, object> Items
        {
            get
            {
                if (noCache || _Items == null)
                {
                    _Items = BuildItems();
                }
                return _Items;
            }
        }
        private static Dictionary<string, object> _Items;

        /*
        public static T GetValue<T>(string key)
        {
            return Items[key].Value<T>();
        }
        */

        public static String[] GetStringList(string key)
        {
            return (Items[key] as object[]).Select(x => x.ToString()).ToArray();
        }

        public static String GetString(string key)
        {
            return Items[key].ToString();
        }

        public static int GetInt(string key)
        {
            return Convert.ToInt32(Items[key]);
        }
    }
}